#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 1e5 + 5;

int n, m;
int a[N];
void pushdown(int p) {
  while (p * 2 <= n) {
    int l = p << 1, r = p << 1 | 1;
    int mx = p;
    if (a[l] > a[mx]) mx = l;
    if (r <= n && a[r] > a[mx]) mx = r;
    if (mx == p) break;
    swap(a[p], a[mx]);
    p = mx;
  }
}
void build_heap() {  // top one is the greatest one
  per(i, n / 2, 1) { pushdown(i); }
}
void pop() {
  swap(a[1], a[n]);
  n--;
  pushdown(1);
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  rep(i, 1, n) cin >> a[i];
  m = n;
  build_heap();
  rep(i, 1, m) pop();
  rep(i, 1, m) cout << a[i] << ' ';
  return 0;
}